/**
 * LY.com Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package quorum.test;

import quorum.AbstractQuorum;
import quorum.DataWrapper;
import quorum.Node;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bixiao.wang
 * @version $Id: MockAbstractQuorum, v 0.1 2021/9/18 17:28 bixiao.wang Exp $
 */
public class MockQuorum extends AbstractQuorum {

    Map<Node, MockNodeServer> nodeServerMap = new HashMap<>();

    public MockQuorum(List<Node> nodeList, int w, int r, boolean readDataWhenJudge) {
        super(nodeList, w, r, readDataWhenJudge);
    }

    @Override
    public boolean init(Node node) {
        nodeServerMap.put(node, new MockNodeServer());
        return true;
    }

    @Override
    public boolean destroy(Node node) {
        nodeServerMap.clear();
        return true;
    }

    @Override
    public boolean doWrite(String key, Node node, DataWrapper dataWrapper) {
        MockNodeServer mockNodeServer = nodeServerMap.get(node);
        return mockNodeServer.write(key, dataWrapper);
    }

    @Override
    public DataWrapper doReadMeta(Node node, String key) {
        MockNodeServer mockNodeServer = nodeServerMap.get(node);
        return mockNodeServer.read(key);
    }

    @Override
    public DataWrapper doReadData(Node node, String key) {
        MockNodeServer mockNodeServer = nodeServerMap.get(node);
        return mockNodeServer.read(key);
    }

    @Override
    public boolean doDeleteData(Node node, String key, Long version) {
        MockNodeServer mockNodeServer = nodeServerMap.get(node);
        return mockNodeServer.delete(key, version);
    }

    @Override
    public boolean doDeleteData(Node node, String key) {
        MockNodeServer mockNodeServer = nodeServerMap.get(node);
        return mockNodeServer.delete(key);
    }
}
